//
//  ClubGameCommiteDetailView.swift
//  77Club
//
//  Created by Apple on 2024/9/24.
//

import UIKit
import Cosmos

class ClubGameDetailCommitTopCell: UITableViewCell {
    
    private let iconImageView = c_make_ex(UIImageView.self)
        .corner(20.0.Scale).mode(.scaleAspectFill).end()
    private let nameLabel = c_make_ex(UILabel.self)
        .color("#1A1A1A").font(12.font_semiBold).end()
    private let timeLabel = c_make_ex(UILabel.self)
        .color("#AFB1B3").font(11.font_medium).end()
    private var starView = CosmosView()
    private let contentLabel = c_make_ex(UILabel.self)
        .color("#4B4C4D").font(15.font_medium).line(0).end()
    private let contentImageV = c_make_ex(UIImageView.self)
        .mode(.scaleAspectFill).corner(8.0.Scale).end()
    private let gameView = c_make_ex(UIView.self)
        .corner(8.0.Scale).bgColor("#4B4C4D").end()
    private let gameImageV = c_make_ex(UIImageView.self)
        .mode(.scaleAspectFill).corner(8.0.Scale).end()
    private let gameNameLabel = c_make_ex(UILabel.self)
        .color(.white).font(12.font_semiBold).end()
    private var gameSubLabel = c_make_ex(UILabel.self)
        .color(.white.alpha(0.85)).font(10.font_medium).end()
    private let commitView = c_make_ex(UIView.self)
        .bgColor(.white).end()
    private let commitLabel = c_make_ex(UILabel.self)
        .color("#323233").font(14.font_semiBold).text("全部评论").end()
    private let commitNumLabel = c_make_ex(UILabel.self)
        .color("#7D7E80").font(12.font_medium).end()
    private let emptyView = c_make_ex(UIView.self)
        .bgColor(.clear).end()
    private let emptyImageView = c_make_ex(UIImageView.self)
        .image("Common_commitEmpty").end()
    private let emptyLabel = c_make_ex(UILabel.self)
        .text("快来成为第一个踊跃发言的伙伴吧~").alignment(.center)
        .color("#4B4C4D").font(14.font_medium).end()
    private let jingImageView = c_make_ex(UIImageView.self)
        .image("GameDetail_jing").mode(.scaleAspectFill).end()
    
    var didSelectImage:((_:Int,_:[String])->())?
    var popBlock:(()->())?
    
    var model: ClubGameDetailCommentListModel? {
        didSet{
            guard let md = model else { return }
            
            iconImageView.loadImage(url: md.touxiang)
            jingImageView.isHidden = !(md.jiajing == "1")
            let paragraphStyle = NSMutableParagraphStyle()
            paragraphStyle.lineSpacing = 6 // 设置行间距
            // 创建属性字符串
            let attributedString = NSMutableAttributedString(string: md.content)
            attributedString.addAttribute(.paragraphStyle, value: paragraphStyle, range: NSRange(location: 0, length: md.content.count))
            contentLabel.attributedText = attributedString
            nameLabel.text = md.user_name
            timeLabel.text = md.timetran
            starView.rating = md.point/2
            if md.images.count > 0 {
                contentImageV.loadImage(url: md.images[0])
                contentImageV.heightConstraint?.constant = 197.0.Scale
            }else{
                contentImageV.heightConstraint?.constant = 0
            }
            if md.son_list.count > 0 {
                commitView.isHidden = false
                emptyView.isHidden = true
                commitView.heightConstraint?.constant = 44.0.Scale
                emptyView.heightConstraint?.constant = 0
                let count = md.son_list.count
                commitNumLabel.text = "(" + "\(count)" + ")"
            }else{
                commitView.isHidden = true
                emptyView.isHidden = false
                commitView.heightConstraint?.constant = 0
                emptyView.heightConstraint?.constant = 270.0.Scale
            }
        }
    }
    
    var gameModel: ClubNewHomeGameModel? {
        didSet{
            guard let md = gameModel else { return }
            
            gameImageV.loadImage(url: md.icon)
            gameNameLabel.text = md.name
            let tagStr = md.biaoqian.joined(separator: " · ")
            var subStr = String(format: "%.1f", md.point)
            subStr = subStr + " · " + tagStr
            gameSubLabel.text = subStr
        }
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        initUI()
    }
    
    func initUI() -> Void {
        
        selectionStyle = UITableViewCell.SelectionStyle.none
        backgroundColor = UIColor.clear
        
        contentView.subviews{
            iconImageView
            nameLabel
            timeLabel
            jingImageView
            starView
            contentLabel
            contentImageV
            gameView.subviews{
                gameImageV
                gameNameLabel
                gameSubLabel
            }
            commitView.subviews{
                commitLabel
                commitNumLabel
            }
            emptyView.subviews{
                emptyImageView
                emptyLabel
            }
        }
        
        iconImageView.leading(12.0.Scale).top(8.0.Scale).size(40.0.Scale)
        nameLabel.leading(64.0.Scale).top(14.0.Scale).trailing(20.0.Scale)
        timeLabel.leading(64.0.Scale).top(32.0.Scale).trailing(20.0.Scale)
        starView.leading(12.0.Scale).width(112.0.Scale).height(16.0.Scale)
        starView.Top == iconImageView.Bottom + 12.0.Scale
        jingImageView.trailing(0).top(50.0.Scale).size(82.0.Scale)
        contentLabel.leading(12.0.Scale).trailing(12.0.Scale)
        contentLabel.Top == starView.Bottom + 8.0.Scale
        contentImageV.leading(12.0.Scale).trailing(12.0.Scale).height(197.0.Scale)
        contentImageV.Top == contentLabel.Bottom + 16.0.Scale
        gameView.leading(12.0.Scale).trailing(12.0.Scale).height(56.0.Scale)
        gameView.Top == contentImageV.Bottom + 16.0.Scale
        gameImageV.leading(12.0.Scale).size(32.0.Scale).centerVertically()
        gameNameLabel.top(11.0.Scale).leading(56.0.Scale)
        gameSubLabel.leading(56.0.Scale).trailing(12.0.Scale).height(16).top(28.0.Scale)
        commitView.leading(0).trailing(0).height(44.0.Scale).bottom(0.Scale)
        commitView.Top == gameView.Bottom + 24.0.Scale
        commitLabel.leading(12.0.Scale).top(20.0.Scale)
        commitNumLabel.leading(72.0.Scale)
        commitNumLabel.Bottom == commitLabel.Bottom
        emptyView.leading(0).trailing(0).height(270.0.Scale)
        emptyView.Top == gameView.Bottom
        emptyImageView.top(56.0.Scale).width(96.0.Scale).height(131.0.Scale).centerHorizontally()
        emptyLabel.leading(0).trailing(0)
        emptyLabel.Top == emptyImageView.Bottom + 12.0.Scale
        
        starView.rating = 0
        starView.settings.starSize = 16.0.Scale
        starView.settings.starMargin = 8.0.Scale
        starView.settings.fillMode = .half
        starView.settings.filledImage = "Common_fillStar".toImage
        starView.settings.emptyImage = "Common_emptyStar".toImage
        starView.isUserInteractionEnabled = false
        
        jingImageView.isHidden = true
        
        let imgTap = UITapGestureRecognizer(target: self, action: #selector(cliclContentImageV))
        contentImageV.isUserInteractionEnabled = true
        contentImageV.addGestureRecognizer(imgTap)
        
        let gameTap = UITapGestureRecognizer(target: self, action: #selector(clickGameView))
        gameView.isUserInteractionEnabled = true
        gameView.addGestureRecognizer(gameTap)
    }
 
    @objc private func cliclContentImageV(){
        guard let images = model?.images, images.count > 0 else { return }
        didSelectImage?(0,images)
    }
    
    @objc private func clickGameView(){
        popBlock?()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
}
